<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <label class="el-form-item-label">支付状态</label>
      <el-select
        v-model="query.paymentStatus"
        clearable
        size="small"
        placeholder="支付状态"
        class="filter-item"
        style="width: 90px"
        @change="crud.toQuery"
      >
        <el-option
          v-for="item in paymentStatusTypeOptions"
          :key="item.key"
          :label="item.display_name"
          :value="item.key"
        />
      </el-select>
      <label class="el-form-item-label">退款状态</label>
      <el-select
        v-model="query.refundStatus"
        clearable
        size="small"
        placeholder="退款状态"
        class="filter-item"
        style="width: 90px"
        @change="crud.toQuery"
      >
        <el-option
          v-for="item in refundStatusTypeOptions"
          :key="item.key"
          :label="item.display_name"
          :value="item.key"
        />
      </el-select>
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="订单号，唯一标识每个订单" prop="orderNumber">
            <el-input v-model="form.orderNumber" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="订单总金额，精确到小数点后两位" prop="totalAmount">
            <el-input v-model="form.totalAmount" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="支付方式，如支付宝、微信支付、银行卡等" prop="paymentMethod">
            <el-input v-model="form.paymentMethod" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="支付状态：0 - 未支付；1 - 已支付；2 - 支付失败" prop="paymentStatus">
            <el-input v-model="form.paymentStatus" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="支付时间，记录实际支付完成的时间">
            <el-input v-model="form.paymentTime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="第三方交易号，例如支付宝交易号">
            <el-input v-model="form.thirdPartyTransactionId" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="退款状态：0 - 未退款；1 - 申请中；2 - 已退款；3 - 退款失败" prop="refundStatus">
            <el-input v-model="form.refundStatus" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="退款金额，如果发生部分退款，这里记录实际退款的金额">
            <el-input v-model="form.refundAmount" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="退款原因，简要描述退款的原因，如“重复支付”、“医生取消预约”等">
            <el-input v-model="form.refundReason" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="退款时间，记录实际退款完成的时间">
            <el-input v-model="form.refundTime" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="orderNumber" label="订单号" width="150" />
        <el-table-column prop="userId" label="用户ID" />
        <el-table-column prop="appointmentId" label="预约ID" />
        <el-table-column prop="totalAmount" label="订单总金额" />
        <el-table-column prop="paymentMethod" label="支付方式，如支付宝、微信支付、银行卡等" />
        <el-table-column prop="paymentStatus" label="支付状态：0 - 未支付；1 - 已支付；2 - 支付失败">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.paymentStatus === 0" type="warning">未支付</el-tag>
            <el-tag v-if="scope.row.paymentStatus === 1" type="success">已支付</el-tag>
            <el-tag v-if="scope.row.paymentStatus === 2" type="danger">支付失败</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="paymentTime" label="支付时间" />
        <el-table-column prop="thirdPartyTransactionId" label="第三方交易号，例如支付宝交易号" />
        <el-table-column prop="refundStatus" label="退款状态：0 - 未退款；1 - 申请中；2 - 已退款；3 - 退款失败">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.refundStatus === 0" type="warning">未退款</el-tag>
            <el-tag v-if="scope.row.refundStatus === 1" type="success">申请中</el-tag>
            <el-tag v-if="scope.row.refundStatus === 2" type="danger">已退款</el-tag>
            <el-tag v-if="scope.row.refundStatus === 3" type="danger">退款失败</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="refundAmount" label="退款金额，如果发生部分退款，这里记录实际退款的金额" />
        <el-table-column prop="refundReason" label="退款原因，简要描述退款的原因，如“重复支付”、“医生取消预约”等" />
        <el-table-column prop="refundTime" label="退款时间，记录实际退款完成的时间" />
        <el-table-column v-if="checkPer(['admin','sysOrder:edit','sysOrder:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudSysOrder from '@/api/system/order'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

const defaultForm = { id: null, userId: null, appointmentId: null, orderNumber: null, totalAmount: null, paymentMethod: null, paymentStatus: null, paymentTime: null, thirdPartyTransactionId: null, refundStatus: null, refundAmount: null, refundReason: null, refundTime: null, createTime: null, updateTime: null }
export default {
  name: 'SysOrder',
  // eslint-disable-next-line vue/no-unused-components
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: '订单管理', url: 'api/sysOrder', idField: 'id', sort: 'id,desc', crudMethod: { ...crudSysOrder }})
  },
  data() {
    return {
      paymentStatusTypeOptions: [
        { key: '0', display_name: '未支付' },
        { key: '1', display_name: '已支付' },
        { key: '2', display_name: '支付失败' }
      ],
      refundStatusTypeOptions: [
        { key: '0', display_name: '未退款' },
        { key: '1', display_name: '申请中' },
        { key: '2', display_name: '已退款' },
        { key: '3', display_name: '退款失败' }
      ],
      permission: {
        add: ['admin', 'sysOrder:add'],
        edit: ['admin', 'sysOrder:edit'],
        del: ['admin', 'sysOrder:del']
      },
      rules: {
        userId: [
          { required: true, message: '用户ID，关联到sys_user表不能为空', trigger: 'blur' }
        ],
        appointmentId: [
          { required: true, message: '预约ID，关联到预约表不能为空', trigger: 'blur' }
        ],
        orderNumber: [
          { required: true, message: '订单号，唯一标识每个订单不能为空', trigger: 'blur' }
        ],
        totalAmount: [
          { required: true, message: '订单总金额，精确到小数点后两位不能为空', trigger: 'blur' }
        ],
        paymentMethod: [
          { required: true, message: '支付方式，如支付宝、微信支付、银行卡等不能为空', trigger: 'blur' }
        ],
        paymentStatus: [
          { required: true, message: '支付状态：0 - 未支付；1 - 已支付；2 - 支付失败不能为空', trigger: 'blur' }
        ],
        refundStatus: [
          { required: true, message: '退款状态：0 - 未退款；1 - 申请中；2 - 已退款；3 - 退款失败不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    }
  }
}
</script>

<style scoped>

</style>
